import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.apache.groovy.json.internal.LazyMap

//  https://www.w3cschool.cn/groovy/groovy_file_io.html
class TestJson {

    public static void main(String[] args) {

        //***************************************************************************
        //***
        //***   JsonSlurper
        //***   {}  :   LazyMap,   空映射 [:]
        //***   []  :   ArrayList, 空集合 []
        //***
        //***************************************************************************
        def jsonSlurper = new JsonSlurper()

        test1(jsonSlurper)
        println()
        println()
        test2(jsonSlurper)
        println()
        println()
        test3(jsonSlurper)
        println()
        println()
        test4(jsonSlurper)
    }

    private static void test1(JsonSlurper jsonSlurper) {
        def object = jsonSlurper.parseText('{ "name": "John", "ID" : "1"}')
        println(object.getClass());
        if (object instanceof LazyMap && object.size() > 0) {
            println(object.name);
            println(object.ID);
        }
    }

    /**
     * 对象转化为JSON
     * 在 Groovy 中提供 JsonOutput 将对象解析为 json 字符串。
     */
    private static void test2(JsonSlurper jsonSlurper) {
        Person person = new Person("xq", 26)
        //将对象生成json
        def json = JsonOutput.toJson(person)
        //JsonOutput.prettyPrint 输出带有 json 格式
        println "0===========================" + json
        println "1===========================" + JsonOutput.prettyPrint(json)

        //解析json为map或list
        def jsonObj = jsonSlurper.parseText(json)
        println "2===========================" + jsonObj.getClass()
        if (jsonObj instanceof Map) {
            println "3===========================" + jsonObj["name"]
        }
    }

    private static void test3(JsonSlurper jsonSlurper) {
        def jsonObj = jsonSlurper.parseText(new File("src", "file\\one.json").text)
        println("=====================================" + jsonObj)
        println("=====================================" + jsonObj.getClass())
        //class java.util.ArrayList
        if (jsonObj instanceof ArrayList && jsonObj.size() > 0) {
            println("=====================================" + jsonObj.size())
            jsonObj.each {
                println("=============================" + it)
                println("=============================" + it.getClass())
                //class org.apache.groovy.json.internal.LazyMap
                if (it instanceof LazyMap && it.size() > 0) {
                    println("=============================" + it.size())
                    it.each { key, value ->
                        println key + "========================" + value
                    }
                }
            }
        }
    }

    private static void test4(JsonSlurper jsonSlurper) {
        def jsonObj = jsonSlurper.parseText(new File("src", "file\\two.json").getText())
        println("00==============================" + jsonObj.getClass())
        //class org.apache.groovy.json.internal.LazyMap

        def children = jsonObj["children"]
        println "20==============================" + children
        println "21==============================" + children.getClass()
        println()
        //class java.util.ArrayList

        def result = children.findAll { item ->
            println "30==============================" + item
            println "31==============================" + item.getClass()
            //class org.apache.groovy.json.internal.LazyMap
            item["id"] == "3088"  //true or false
        }
        println "40==============================" + result
    }
}

class Person implements Serializable {

    private String name
    private int age

    Person(String name, int age) {
        this.name = name
        this.age = age
    }

    String getName() {
        return name
    }

    void setName(String name) {
        this.name = name
    }

    int getAge() {
        return age
    }

    void setAge(int age) {
        this.age = age
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
